import { RootState } from '@/types/store'
import { useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'

// stateName只能从 项目的整个 redux 中 的模块的名字来取
// reducer/index.ts ===> {login, profile }
export default function useInitialState<K extends keyof RootState> (action:()=>void, stateName: K) {
  // 发请求-拿数据-保存redxu
  const dispatch = useDispatch()
  useEffect(() => {
    dispatch(action())
  }, [dispatch])
  // 从redux拿数据
  return useSelector((state: RootState) => state[stateName])
}
